package cn.com.cetccst.mdm.engine;

import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.widget.Toast;

import com.oplus.enterprise.mdmcoreservice.manager.DeviceApplicationManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceConnectivityManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceControlerManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceNetworkManager;
import com.oplus.enterprise.mdmcoreservice.manager.DevicePackageManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceRestrictionManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceSettingsManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceStateManager;
import com.oplus.enterprise.mdmcoreservice.manager.DeviceVpnManager;

import java.util.ArrayList;
import java.util.List;

public class OPPOMdmEngine extends BaseMDMEngine {

    private static final String TAG = "HwMdmEngine";
    private DeviceApplicationManager deviceApplicationManager;
    private DevicePackageManager devicePackageManager;
    private DeviceSettingsManager settingsManager;
    private DeviceRestrictionManager restrictionManager;
    private DeviceVpnManager vpnManager;
    private DevicePolicyManager dpm;
    private DeviceControlerManager deviceControlerManager;
    private DeviceConnectivityManager deviceConnectivityManager;
    private DeviceNetworkManager deviceNetworkManager;
    private DeviceStateManager deviceStateManager;

    public OPPOMdmEngine(Context context) {
        super(context);
        deviceApplicationManager = DeviceApplicationManager.getInstance(context);
        settingsManager = DeviceSettingsManager.getInstance(context);
        restrictionManager = DeviceRestrictionManager.getInstance(context);
        vpnManager = DeviceVpnManager.getInstance(context);
        devicePackageManager = DevicePackageManager.getInstance(context);
        deviceControlerManager = DeviceControlerManager.getInstance(context);
        deviceConnectivityManager = DeviceConnectivityManager.getInstance(context);
        deviceNetworkManager = DeviceNetworkManager.getInstance(context);
        deviceStateManager = DeviceStateManager.getInstance(context);
    }

    @Override
    public void controlBluetooth(boolean disabled) {
        restrictionManager.setBluetoothDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isBluetoothControlled() {
        return restrictionManager.isBluetoothDisabled(adminComponent);
    }

    @Override
    public boolean setBluetoothWhiteList(List<String> whiteList) {
        deviceConnectivityManager.removeBluetoothDevicesFromWhiteList(adminComponent, getBluetoothWhiteList());
        if (whiteList != null && whiteList.size() > 0) {
            deviceConnectivityManager.addBluetoothDevicesToWhiteList(adminComponent, whiteList);
        }
        return true;
    }

    @Override
    public List<String> getBluetoothWhiteList() {
        return deviceConnectivityManager.getBluetoothDevicesFromWhiteLists(adminComponent);
    }

    @Override
    public void controlCamera(boolean disabled) {
        restrictionManager.setCameraPolicies(disabled ? 0 : 1);
    }

    @Override
    public boolean isCameraControlled() {
        return restrictionManager.getCameraPolicies() == 0;
    }

    @Override
    public boolean setUninstallblockApps(List<String> packageNames) {
        if (packageNames.isEmpty()) {
            devicePackageManager.removeAllDisallowedUninstallPackages(adminComponent);
        } else {
            devicePackageManager.addDisallowedUninstallPackages(adminComponent, packageNames);
        }

        return true;
    }

    @Override
    public List<String> getUninstallblockApps() {
        return devicePackageManager.getDisallowUninstallPackageList(adminComponent);
    }

    @Override
    public void controlWifi(boolean disabled) {
        restrictionManager.setWifiDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isWifiControlled() {
        return restrictionManager.isWifiDisabled(adminComponent);
    }

    @Override
    public boolean setWifiWhiteList(List<String> whiteList) {
        deviceConnectivityManager.removeBSSIDFromWhiteList(new ArrayList<>(whiteList));
        if (whiteList != null && whiteList.size() > 0) {
            return deviceConnectivityManager.addBSSIDToWhiteList(new ArrayList<>(whiteList));
        }
        return true;
    }

    @Override
    public List<String> getWifiWhitList() {
        return deviceConnectivityManager.getBSSIDWhiteList();
    }

    @Override
    public void controlMicroPhone(boolean disabled) {
        restrictionManager.setMicrophonePolicies(adminComponent, disabled ? 0 : 1);
    }

    @Override
    public boolean isMicroPhoneControlled() {
        return restrictionManager.getMicrophonePolicies(adminComponent) == 0;
    }

    @Override
    public void controlExternalStorage(boolean disabled) {
        restrictionManager.setExternalStorageDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isExternalStorageControlled() {
        return restrictionManager.isExternalStorageDisabled(adminComponent);
    }

    @Override
    public void controlScreenShot(boolean disabled) {

        restrictionManager.setScreenCaptureDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isScreenShotControlled() {
        return restrictionManager.isScreenCaptureDisabled(adminComponent);
    }

    @Override
    public void controlNFC(boolean disabled) {
        restrictionManager.setNFCDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isNFCControlled() {
        return restrictionManager.isNFCDisabled(adminComponent);
    }

    @Override
    public void controlMobileHotSpot(boolean disabled) {
        deviceConnectivityManager.setWifiApDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isMobileHotSpotControlled() {
        return deviceConnectivityManager.isWifiApDisabled(adminComponent);
    }

    @Override
    public void controlCallout(boolean disabled) {
        restrictionManager.setVoiceOutgoingDisable(adminComponent, disabled);
    }

    @Override
    public boolean isCalloutControlled() {
        return restrictionManager.isVoiceOutgoingDisabled(adminComponent, 2);
    }

    @Override
    public void controlSms(boolean disabled) {
        restrictionManager.setMmsDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isSmsControlled() {
        return restrictionManager.isMmsDisabled(adminComponent);
    }

    @Override
    public void controlMobileData(boolean disabled) {
        restrictionManager.setMobileDataMode(adminComponent, disabled ? 0 : 2);
    }

    @Override
    public boolean isMobileDataControlled() {
        return restrictionManager.getMobileDataMode(adminComponent) == 0;
    }

    @Override
    public void controlVolumeDown(boolean disabled) {
        //注意，这里 0,1相对其他接口是反的
        settingsManager.setVolumeChangeActionState(adminComponent, disabled ? 1 : 0);
    }

    @Override
    public boolean isVolumeDownControlled() {
        return settingsManager.getVolumeChangeActionState(adminComponent) == 1;
    }

    @Override
    public void controlAutoTime(boolean disabled) {
        settingsManager.setTimeAndDateSetDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isAutoTimeControlled() {
        return settingsManager.isTimeAndDateSetDisabled(adminComponent);
    }

    @Override
    public void controlUsbData(boolean disabled) {
        restrictionManager.setUSBDataDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isUsbDataControlled() {
        return restrictionManager.isUSBDataDisabled(adminComponent);
    }

    @Override
    public void controlUsbDebug(boolean disabled) {
        restrictionManager.setAdbDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isUsbDebugControlled() {
        return restrictionManager.isAdbDisabled(adminComponent);
    }

    @Override
    public void controlRestoreFactory(boolean disabled) {
        settingsManager.setRestoreFactoryDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isRestoreFactoryControlled() {
        return settingsManager.isRestoreFactoryDisabled(adminComponent);
    }

    @Override
    public void controlSystemUpgrade(boolean disabled) {
        restrictionManager.setSystemUpdatePolicies(adminComponent, disabled ? 0 : 1);
    }

    @Override
    public boolean isSystemUpgradeControlled() {
        return restrictionManager.getSystemUpdatePolicies(adminComponent) == 0;
    }

    @Override
    public void controlGPS(boolean disabled) {
        deviceConnectivityManager.setGPSDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isGPSControlled() {
        return deviceConnectivityManager.isGPSDisabled(adminComponent);
    }

    @Override
    public void controlUnknownSource(boolean disabled) {
//        restrictionManager.setUnknownSourceAppInstallDisabled(adminComponent, disabled);

        deviceApplicationManager.enableTrustedAppStore(adminComponent, disabled);
        if (disabled) {
            deviceApplicationManager.addTrustedAppStore(adminComponent, "com.zdk.mg.agent");
        }
    }

    @Override
    public boolean isUnknownSourceControlled() {
        return deviceApplicationManager.isTrustedAppStoreEnabled(adminComponent);
    }

    @Override
    public void controlCopyPaste(boolean disabled) {
        restrictionManager.disableClipboard(adminComponent, disabled);
    }

    @Override
    public boolean isCopyPasteControlled() {
        return restrictionManager.isClipboardDisabled();
    }

    @Override
    public void controlFingerPrint(boolean disabled) {
        restrictionManager.setUnlockByFingerprintDisabled(adminComponent, disabled);
    }

    @Override
    public boolean isFingerPrintControlled() {
        return restrictionManager.isUnlockByFingerprintDisabled(adminComponent);
    }

    @Override
    public boolean setDisallowRunningApps(List<String> packageNames, boolean isBlock) {
        if (isBlock) {
            //黑名单模式。清空白名单
            deviceApplicationManager.clearAllowedRunningApp();
            packageNames.remove("com.zdk.mg.agent");
            deviceApplicationManager.addDisallowedRunningApp(adminComponent, packageNames);
        } else {
            //白名单模式。清空黑名单
            deviceApplicationManager.removeAllDisallowedRunningApp(adminComponent);
            packageNames.add(0, "com.zdk.mg.agent");
            deviceApplicationManager.addAllowedRunningApp(packageNames);
        }
        return false;
    }

    @Override
    public List<String> getDisallowRunningApps() {
        return deviceApplicationManager.getDisallowedRunningApp(adminComponent);
    }

    @Override
    public void installPackage(String apkPath) {
        devicePackageManager.installPackage(adminComponent, apkPath);
    }

    @Override
    public void uninstallPackage(String packageName) {
        devicePackageManager.uninstallPackage(adminComponent, packageName, false);
    }

    @Override
    public void formatSDCard() {
        boolean result = deviceControlerManager.formatSDCard(adminComponent);
        Toast.makeText(context, "formatSDCard result:" + result, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void factoryReset() {
        deviceControlerManager.wipeDeviceData();
//        devicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);

    }

    @Override
    public boolean setInstallSourceApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getInstallSourceApps() {
        return null;
    }

    @Override
    public boolean setNetworkWhiteListApps(List<String> packageNames) {
        return deviceNetworkManager.addAppMeteredDataBlackList(adminComponent, packageNames) && deviceNetworkManager.addAppWlanDataBlackList(adminComponent, packageNames);
    }

    @Override
    public List<String> getNetworkWhiteListApps() {
        return deviceNetworkManager.getAppWlanDataBlackList(adminComponent);
    }

    @Override
    public boolean setKeepLiveApps(List<String> packageNames) {
        List<String> apps = getKeepLiveApps();
        deviceApplicationManager.removePersistentApp(adminComponent, apps);
        deviceApplicationManager.addPersistentApp(adminComponent, packageNames);
//        deviceStateManager.ignoringBatteryOptimizations(adminComponent,packageNames);
        return true;
    }

    @Override
    public List<String> getKeepLiveApps() {
        return deviceApplicationManager.getPersistentApp(adminComponent);
    }

    @Override
    public boolean setSuperApps(List<String> packageNames) {
        return false;
    }

    @Override
    public List<String> getSuperApps() {
        return null;
    }

    private static final byte[] token = "12345678901234561234567890123456".getBytes();

    @Override
    public boolean setDevicePassword(String password) {
//        boolean succeed = false;
//        if ((SDK_INT >= Build.VERSION_CODES.O)) {
//            boolean isResetPasswordTokenActive = devicePolicyManager.isResetPasswordTokenActive(adminComponent);
//            Logger.log(TAG, "isResetPasswordTokenActive " + isResetPasswordTokenActive);
//            succeed = devicePolicyManager.resetPasswordWithToken(adminComponent, password, token, 0);
//            devicePolicyManager.lockNow();
//        } else {
//            succeed = devicePolicyManager.resetPassword(password, 0);
//        }
//        Logger.log(TAG, "try to reset device password on Android N+ " + succeed + " " + password);

        return true;
    }

    @Override
    public String getName() {
        return "Oppo";
    }
}
